<template>
  <div class="app-container">
    <div v-if="user">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <user-card :user="user" />
        </el-col>
        <el-col :span="18" :xs="24">
          <el-card>
            <el-tabs v-model="activeTab">
              <el-tab-pane label="修改密码" name="ChangePassword">
                <ChangePassword @updatePassword="updatePwd" />
              </el-tab-pane>
              <el-tab-pane label="个人信息" name="account">
                <userupdate :user="user" @submitForm="submitForms"/>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UserCard from "./components/UserCard";
import ChangePassword from "./components/ChangePassword";
import Account from "./components/Account";
import userupdate from "./components/userupdate";

import { getInfo,updateById,changePassword } from "@/api/user";
export default {
  name: "Profile",
  components: { UserCard,userupdate, ChangePassword, Account },
  data() {
    return {
      user: {},
      activeTab: "ChangePassword",
    };
  },
  computed: {
    ...mapGetters([
      "name",
      "avatar",
      // ,
      // 'roles'
    ]),
  },
  created() {
    this.getUser();
  },
  methods: {
    async getUser() {
      let location = await getInfo();
      this.user = location.data.user;
    },
    submitForms(users){
      updateById(users).then(()=>{
          this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.getUser()
      })
    },
    // 修改密码
    updatePwd(users){
       console.log(users)
      changePassword(users).then(()=>{
          this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.getUser()
      })
    }
  },
};
</script>
